<?php //strip
/**
 * Helper class for easy rendering of administration UI.
 *
 * @package odWpPlugin
 * @author Ondrej Donek <ondrejd@gmail.com>
 */
class odWpPlugin_Admin_Renderer
{
  /** 
   * Holds admin messages
   * 
   * @var array 
   */
  protected $mAdminMessages = array();
  
  /** 
   * Holds identifier of the parent plug-in.
   * 
   * @var string 
   */
  protected $mPluginId;
  
  /** 
   * Holds name of the parent plug-in.
   * 
   * @var string 
   */
  protected $mName;
  
  /** 
   * Holds name of class that implements of the parent plug-in.
   * 
   * @var string 
   */
  protected $mPluginClassname;
  
  /** 
   * Holds version of the parent plug-in.
   * 
   * @var string 
   */
  protected $mPluginVersion;
  
  /**
   * Holds array describing admin menu.
   * 
   * @var array
   */
  protected $mMenu;
	
	/**
	 * Constructor.
	 *
	 * @param array $aOptions
	 * @return void
	 */
	public function __construct($aOptions = array())
	{
    if(!array_key_exists('plugin_id', $aOptions) || 
       !array_key_exists('plugin_name', $aOptions) || 
       !array_key_exists('plugin_classname', $aOptions) || 
       !array_key_exists('plugin_version', $aOptions)) {
      throw new Exception(
        'Constructor of odWpPlugin_Admin_Renderer needs `plugin_id`, ' . 
        '`plugin_name`, `plugin_classname` and `plugin_version` defined ' . 
        'in passed options!.'
      );
    }
    
    $this->mPluginId = $aOptions['plugin_id'];
    $this->mPluginName = $aOptions['plugin_name'];
    $this->mPluginClassname = $aOptions['plugin_classname'];
    $this->mPluginVersion = $aOptions['plugin_version'];
    
		if(array_key_exists('stylesheet', $aOptions)) {
			if($aOptions['stylesheet'] === true) {
				add_action('admin_head', array(&$this, 'addStylesheet'));
			}
		}
    
    if(array_key_exists('menu', $aOptions)) {
      $this->mMenu = $aOptions['menu'];
      add_action('admin_menu', array(&$this, 'addMenu'));
    }
	} // end __construct($aOptions)
  
  
	/**
	 * Registers administration menu for the plugin
	 * 
	 * @return void
	 */
	function addMenu() 
	{
		add_menu_page(
        __($this->mPluginName, $this->mPluginClassname), 
        __($this->mPluginName, $this->mPluginClassname), 
        'edit_pages',
        $this->mPluginId,
        $this->mMenu['self'],
        WP_PLUGIN_URL . '/' . $this->mPluginId . '/icon16.png'
    );
    
    foreach($this->mMenu as $menuitem_key => $menuitem) {
      if($menuitem_key == 'self' || !is_array($menuitem)) continue;
      
      $page_title = (array_key_exists('page_title', $menuitem)) 
          ? $menuitem['page_title'] 
          : $this->mPluginName . ' - ' . $menuitem['menu_title'];
      
      add_submenu_page(
          $this->mPluginId,
          // TODO Kdyz 'label' chybi, tak ho slozit ze short_label a plugin_name
          $menuitem['page_title'],
          $menuitem['menu_title'],
          $menuitem['capability'],
          $menuitem['menu_slug'],
          $menuitem['controller']
      );
    }
	} // end addMenu()
  
  
  /**
   * Appends given stylesheet to the admin header.
   *
   * @param string $aCssFile Optional. Defaultly 'wp-admin.css'.
   * @returns void
   */
  public function addStylesheet($aCssFile = 'wp-admin.css')
  {
    $url = get_bloginfo('home') . '/wp-content/plugins/' . 
        $this->mPluginId . '/resources/wp-admin.css';
    
    echo '<link rel="stylesheet" type="text/css" href="' . $url . '" />';
  } // end addStylesheet($aCssFile)
  
  /**
   * Add additional JavaScript or CSS resources to the header of WordPress 
   * administration.
   * 
   * @param string|array $aScripts
   * @param string|array $aStyles
   * @return void
   */
  public function addResources($aScripts = null, $aStyles = null)
  {
    if(is_string($aScripts)) {
      echo '<script type="text/javascript" src="' . $aScripts . '"></script>' . PHP_EOL;
    }
    elseif(is_array($aScripts)) {
      foreach($aScripts as $aScript) {
        echo '<script type="text/javascript" src="' . $aScript . '"></script>' . PHP_EOL;
      }
    }
    
    if(is_string($aStyles)) {
      echo '<link rel="stylesheet" type="text/css" href="' . $aStyles . '"/>' . PHP_EOL;
    }
    elseif(is_array($aStyles)) {
      foreach($aStyles as $aStyle) {
        echo '<link rel="stylesheet" type="text/css" href="' . $aStyle . '"/>' . PHP_EOL;
      }
    }
  }
  
  /**
   * Prints admin user message.
   * 
   * @param string $aMessage Text of the massage
   * @param string $aType Optional. Defaultly 'updated'.
   * @param string $aIdSuffix Optional. Defaultly empty.
   * @return void
   */
  public function adminMsg($aMessage, $aType = 'updated', $aIdSuffix = '') 
  {
    $this->mAdminMessages[] = sprintf(
      '<div id="message%1$s" class="%2$s fade"><p>%3$s</p></div>', 
      $aIdSuffix, $aType, $aMessage
    );
  }
  
  /**
   * Prints header for the admin page.
   * 
   * @param string $aTitle Title of the page.
   * @param string $aIcon URL to the page's icon.
   * @return void
   */
  public function adminPageHeader($aTitle, $aIcon = '')
  {
?>
<div id="wpbody-content">
<div class="wrap">
  <?php if(!empty($aIcon)):?>
    <div class="icon32">
      <img src="<?php echo $aIcon;?>"/>
    </div>
  <?php endif;?>
  <h2><?php echo $aTitle;?></h2>
  <?php foreach($this->mAdminMessages as $admin_msg):?>
    <?php echo $admin_msg;?>
  <?php endforeach;?>
<?php
  }
  
  /**
   * Prints footer for the admin page.
   * 
   * @return void
   */
  public function adminPageFooter()
  {
?>
</div>
</div>
<?php
  }
  
  /**
   * Prints complete page for WordPress administration.
   * 
   * @param string $aTitle Title of the page.
   * @param string $aContent HTML content of the page.
   * @param string $aIcon URL to the page's icon.
   * @return void
   */
  public function adminPage($aTitle, $aContent, $aIcon = '')
  {
    $this->adminPageHeader($aTitle, $aIcon);
    
    echo $aContent;
    
    $this->adminPageFooter();
  }
  
  /** 
   * Prints plug-in's settings page.
   * 
   * @param string $aTitle Title of the options page.
   * @param string $aIcon URL to the page's icon.
   * @param array $aOptions
   * @return void
   * @todo Finish this!
   */
  public function adminSettingsPage($aTitle, $aIcon = '', $aOptions = array())
  {
    $this->adminPageHeader($aTitle, $aIcon);
    
    $options_form = new odWpPlugin_Form(
      'settings-page', 
      '',
      $aOptions, 
      odWpPlugin_Form::DECORATOR_TABLE,
      null
    );
    $options_form->render();
    
    $this->adminPageFooter();
  }
  
} // End of odWpPlugin_Admin_Renderer
